<template>
  <div class="basic_info">
    <div class="info_operation_btn">
      <div class="btn_left">
        <el-button type="primary" @click="onRefresh">统计</el-button>
        <el-button style="width: 1.2rem" @click="onLookDetails" :disabled="total === 0 || isGroupByGoodsType"
          :class="total === 0 || isGroupByGoodsType ? 'disabled_button' : ''">查看明细</el-button>
        <el-button @click="exportExcel()" :disabled="!buttonsPermissions.DaoChu"
          :class="!buttonsPermissions.DaoChu ? 'disabled_button' : ''">导出</el-button>
        <el-button :disabled="!buttonsPermissions.DaYin" :class="!buttonsPermissions.DaYin ? 'disabled_button' : ''"
          @click="handleShortcutKeyPrint">打印(P)</el-button>
      </div>
      <div class="btn_right">
        <el-input v-model="queryData.keywords" placeholder="请输入商品名称\条码\编码\规格\拼音码"></el-input>
        <el-button type="primary" @click="onQuery">搜索</el-button>
        <!-- <el-button @click="onRefresh">刷新</el-button> -->
        <i @click="onShowColumn()" class="set_up el-icon-s-tools"></i>
      </div>
    </div>
    <div class="info_content_div">
      <div class="div_screen">
        <div class="screen_left">
          <div>
            <p>仓库</p>
            <el-select placeholder="" v-model="queryData.storehouseId" class="screen_select" @change="onChangeStorehouse" ref="cangku"
              filterable
              @visible-change="(value) => handleFilterMethod('', 'storehouseName')"
              :filter-method="(value) => handleFilterMethod(value, 'storehouseName')" >
              <el-option v-for="item in ckOptions" :key="item.id" :label="item.storehouseName" :value="item.id">
              </el-option>
            </el-select>
          </div>
          <div>
            <el-checkbox v-model="isGroupByGoodsType" @change="typeChange">显示商品类别的统计结果</el-checkbox>
          </div>
        </div>
        <div class="screen_right">
          <div>
            <p>双击商品可查看库存变动明细</p>
            <el-select @change="changeDate" v-model="monthValue">
              <el-option v-for="item in monthOptions" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
            <el-date-picker v-if="isShowDate" v-model="nowDate" type="daterange" range-separator="~"
              start-placeholder="开始日期" end-placeholder="结束日期" @change="onClickPicker" value-format="yyyy-MM-dd">
            </el-date-picker>
            <el-button type="primary" @click="onQuery">查询</el-button>
          </div>
        </div>
      </div>
      <div class="div_table">
        <div class="div_tree">
          <div class="tree_div1">
            <p>商品类别</p>
          </div>
          <div class="tree_input">
            <p>
              <input type="text" placeholder="查找" v-model="lookupValue" />
              <span>
                <i class="el-icon-search"></i>
              </span>
            </p>
          </div>
          <div class="tree_structure">
            <el-tree ref="tree" :data="treeData" node-key="label" highlight-current default-expand-all :expand-on-click-node="false"
              @node-click="handleNodeClick" :filter-node-method="filterNode">
            </el-tree>
          </div>
        </div>
        <div class="table_content">
          <el-table border show-summary :summary-method="getSummaries" highlight-current-row :header-cell-style="{
            background: '#E3F6FF',
            color: '#666666',
            textAlign: 'center',
            height: '30px',
            borderColor: '#dddddd',
          }" :data="tableData" height="100%" @row-dblclick="handleRowDbclick" @row-click="rowClick" ref="tableData"
            style="width: 100%" @selection-change="handleSelectionChange" @sort-change="changeSort">
            <el-table-column :label="item.label" v-for="(item, index) in columnData" :key="index">
              <el-table-column border :show-overflow-tooltip="true" v-for="(v, i) in item.subset" :key="i" :prop="v.props"
                :label="v.zidingyiName ? v.zidingyiName : v.morenName" min-width="120px"
                :resizable="v.props=='goodsId_dictText'?true:false"
                :width="v.props == 'xh' ? '50px' : v.props == 'spbh' ? '170px':''">
                <template slot-scope="scope">
                  <!-- 商品编码 -->
                  <span v-if="v.props == 'goodsNumber'">{{ goodsNumberFormat(scope.row) }}</span>
                  <!-- 商品名称 -->
                  <span v-else-if="v.props == 'goodsId_dictText'" style="width: 100%; white-space: normal;
  word-wrap: break-word;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical; text-align: left;">{{ goodsNameFormat(scope.row) }}</span>
                  <!-- 期末数量 -->
                  <span v-else-if="v.props == 'finalNumber'">{{ finalNumberFormat(scope.row) }}</span>
                  <!-- 期末金额 20240919修改为后端计算 -->
                  <!-- <span v-else-if="v.props == 'finalAccount'">{{ finalAccountFormat(scope.row) }}</span> -->
                  <!-- 金额 -->
                  <span
                    v-else-if="v.props == 'beforeAccount' || v.props == 'beforeTransportFee' || v.props == 'buyInAccount' || v.props == 'buyExchangeInAccount' || v.props == 'saleBackInAccount' || v.props == 'saleExchangeInAccount' || v.props == 'beyondInAccount' || v.props == 'allotInAccount' || v.props == 'priceChangeInAccount' || v.props == 'sumInAccount' || v.props == 'transportFee' || v.props == 'buyBackOutAccount' || v.props == 'buyExchangeOutAccount' || v.props == 'saleOutAccount' || v.props == 'saleExchangeOutAccount' || v.props == 'lossOutAccount' || v.props == 'allotOutAccount' || v.props == 'priceChangeOutAccount' || v.props == 'sumOutAccount' || v.props == 'finalTransportFee'">{{
                      $formatMoney(scope.row[v.props]) }}</span>
                  <!-- 数量 -->
                  <span
                    v-else-if="v.props == 'beforeNumber' || v.props == 'buyInNumber' || v.props == 'buyExchangeInNumber' || v.props == 'saleBackInNumber' || v.props == 'saleExchangeInNumber' || v.props == 'beyondInNumber' || v.props == 'allotInNumber' || v.props == 'priceChangeInNumber' || v.props == 'sumInNumber' || v.props == 'buyBackOutNumber' || v.props == 'buyExchangeOutNumber' || v.props == 'saleOutNumber' || v.props == 'saleExchangeOutNumber' || v.props == 'lossOutNumber' || v.props == 'allotOutNumber' || v.props == 'priceChangeOutNumber' || v.props == 'sumOutNumber'">{{
                      $formatNumber(scope.row[v.props]) }}</span>
                  <!-- 其它列 -->
                  <span v-else>{{ commonFormat(scope.row, v.props) }}</span>
                </template>
              </el-table-column>
            </el-table-column>
          </el-table>
        </div>
      </div>
      <div class="table_pagination">
        <div class="pagination_div">
          <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
            :current-page="queryData.pageNo" :page-sizes="[20, 50, 100, 200]" :page-size="queryData.pageSize"
            layout="jumper, prev, pager,next, sizes" :total="total">
          </el-pagination>
        </div>
      </div>
    </div>
    <!-- 列配置弹框 -->
    <CConfiguration ref="setColumnRef" :columnDataArr="columnData" orderType="商品进销存表"
      :defaultColumnDataArr="defaultColumnData" @onSaveColumn="onSaveColumn">
    </CConfiguration>
    <!-- 打印 -->
    <dialog-print ref="print"></dialog-print>
  </div>
</template>

<script>
import { getAction } from '@/api/manage'
import { mapGetters } from 'vuex'
import CConfiguration from '@/components/c_column/columnConfiguration1'
import { dateStrFromDate } from '@/utils/DateFormatter'
/**打印 */
import dialogPrint from '@/views/components/dialogPrintStatic'
// 导入调用处理权限数据方法
import { handlePermissions, handleOtherPerms, formatMoney, formatPrice, formatNumber, delcommafy } from '@/utils/index'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'

export default {
  name: 'commodityJXC',
  components: {
    CConfiguration,
    dialogPrint,
  },
  computed: {
    ...mapGetters(['name']),
    buttonsPermissions() {
      return handlePermissions('商品进销存表')
    },
    printPermissions() {
      return handleOtherPerms('单据可多次打印')
    },
  },
  created() {
    this.initData()
  },
  mixins: [JeecgListMixin],
  updated() {
    this.$nextTick(() => {
      this.$refs.tableData.doLayout()
    })
  },
  watch: {
    lookupValue(val) {
      this.$refs.tree.filter(val)
    },
  },
  data() {
    return {
      // 仓库
      ckOptions: [],
      // 查询条件数据
      queryData: {
        //搜索框值
        keywords: '',
        storehouseId: '',
        startDate: '',
        finishDate: '',
        goodsTypeId: '',
        pageSize: 20, //每页的数据条数
        pageNo: 1, //默认开始页面
      },
      storehouseName: '',// 选择的仓库名称
      monthValue: '本月',
      monthOptions: [
        {
          value: '今日',
          label: '今日',
        },
        {
          value: '本周',
          label: '本周',
        },
        {
          value: '本月',
          label: '本月',
        },
        {
          value: '本年',
          label: '本年',
        },
      ],
      isShowDate: true, // 是否显示选择时间组件
      nowDate: [],
      // 表格数据
      tableData: [],
      // 列配置数据
      columnData: [
        {
          props: 'spxx',
          label: '商品信息',
          subset: [
            {
              id: '1',
              morenName: '序号',
              zidingyiName: '',
              morenState: true,
              zidingyiState: true,
              morenSort: 0,
              zidingyiSort: 0,
              noModify: true,
              props: 'xh',
            },
            {
              id: '2',
              morenName: '商品编码',
              zidingyiName: '',
              morenState: true,
              zidingyiState: true,
              morenSort: 1,
              zidingyiSort: 1,
              noModify: false,
              props: 'goodsNumber',
            },
            {
              id: '3',
              morenName: '商品名称',
              zidingyiName: '',
              morenState: true,
              zidingyiState: true,
              morenSort: 2,
              zidingyiSort: 2,
              noModify: false,
              props: 'goodsId_dictText',
            },
            {
              id: '4',
              morenName: '规格',
              zidingyiName: '',
              morenState: true,
              zidingyiState: true,
              morenSort: 3,
              zidingyiSort: 3,
              noModify: false,
              props: 'norms',
            },
            {
              id: '5',
              morenName: '单位',
              zidingyiName: '',
              morenState: true,
              zidingyiState: true,
              morenSort: 4,
              zidingyiSort: 4,
              noModify: false,
              props: 'unit',
            },
          ],
        },
        {
          props: 'cqjy',
          label: '此前结余',
          subset: [
            {
              id: '1',
              morenName: '此前数量',
              zidingyiName: '',
              morenState: true,
              zidingyiState: true,
              morenSort: 0,
              zidingyiSort: 0,
              noModify: false,
              props: 'beforeNumber',
            },
            {
              id: '2',
              morenName: '此前金额',
              zidingyiName: '',
              morenState: true,
              zidingyiState: true,
              morenSort: 1,
              zidingyiSort: 1,
              noModify: false,
              props: 'beforeAccount',
            },
            {
              id: '3',
              morenName: '此前运费',
              zidingyiName: '',
              morenState: true,
              zidingyiState: true,
              morenSort: 2,
              zidingyiSort: 2,
              noModify: false,
              props: 'beforeTransportFee',
            },
          ],
        },
        {
          props: 'qjsprk',
          label: '期间商品入库',
          subset: [
            {
              id: '1',
              morenName: '采购入库数量',
              zidingyiName: '',
              morenState: true,
              zidingyiState: true,
              morenSort: 0,
              zidingyiSort: 0,
              noModify: false,
              props: 'buyInNumber',
            },
            {
              id: '2',
              morenName: '采购入库金额',
              zidingyiName: '',
              morenState: true,
              zidingyiState: true,
              morenSort: 1,
              zidingyiSort: 1,
              noModify: false,
              props: 'buyInAccount',
            },
            {
              id: '3',
              morenName: '采购换货入库数量',
              zidingyiName: '',
              morenState: true,
              zidingyiState: true,
              morenSort: 2,
              zidingyiSort: 2,
              noModify: false,
              props: 'buyExchangeInNumber',
            },
            {
              id: '4',
              morenName: '采购换货入库金额',
              zidingyiName: '',
              morenState: true,
              zidingyiState: true,
              morenSort: 3,
              zidingyiSort: 3,
              noModify: false,
              props: 'buyExchangeInAccount',
            },
            {
              id: '5',
              morenName: '销售退货数量',
              zidingyiName: '',
              morenState: true,
              zidingyiState: true,
              morenSort: 4,
              zidingyiSort: 4,
              noModify: false,
              props: 'saleBackInNumber',
            },
            {
              id: '6',
              morenName: '销售退货金额',
              zidingyiName: '',
              morenState: true,
              zidingyiState: true,
              morenSort: 5,
              zidingyiSort: 5,
              noModify: false,
              props: 'saleBackInAccount',
            },
            {
              id: '7',
              morenName: '销售换货入库数量',
              zidingyiName: '',
              morenState: true,
              zidingyiState: true,
              morenSort: 6,
              zidingyiSort: 6,
              noModify: false,
              props: 'saleExchangeInNumber',
            },
            {
              id: '8',
              morenName: '销售换货入库金额',
              zidingyiName: '',
              morenState: true,
              zidingyiState: true,
              morenSort: 7,
              zidingyiSort: 7,
              noModify: false,
              props: 'saleExchangeInAccount',
            },
            {
              id: '9',
              morenName: '报溢数量',
              zidingyiName: '',
              morenState: true,
              zidingyiState: true,
              morenSort: 8,
              zidingyiSort: 8,
              noModify: false,
              props: 'beyondInNumber',
            },
            {
              id: '10',
              morenName: '报溢金额',
              zidingyiName: '',
              morenState: true,
              zidingyiState: true,
              morenSort: 9,
              zidingyiSort: 9,
              noModify: false,
              props: 'beyondInAccount',
            },
            {
              id: '11',
              morenName: '调拨入库数量',
              zidingyiName: '',
              morenState: true,
              zidingyiState: true,
              morenSort: 10,
              zidingyiSort: 10,
              noModify: false,
              props: 'allotInNumber',
            },
            {
              id: '12',
              morenName: '调拨入库金额',
              zidingyiName: '',
              morenState: true,
              zidingyiState: true,
              morenSort: 11,
              zidingyiSort: 11,
              noModify: false,
              props: 'allotInAccount',
            },
            {
              id: '13',
              morenName: '调价入库数量',
              zidingyiName: '',
              morenState: true,
              zidingyiState: true,
              morenSort: 12,
              zidingyiSort: 12,
              noModify: false,
              props: 'priceChangeInNumber',
            },
            {
              id: '14',
              morenName: '调价入库金额',
              zidingyiName: '',
              morenState: true,
              zidingyiState: true,
              morenSort: 13,
              zidingyiSort: 13,
              noModify: false,
              props: 'priceChangeInAccount',
            },
            {
              id: '15',
              morenName: '入库数量合计',
              zidingyiName: '',
              morenState: true,
              zidingyiState: true,
              morenSort: 14,
              zidingyiSort: 14,
              noModify: false,
              props: 'sumInNumber',
            },
            {
              id: '16',
              morenName: '入库金额合计',
              zidingyiName: '',
              morenState: true,
              zidingyiState: true,
              morenSort: 15,
              zidingyiSort: 15,
              noModify: false,
              props: 'sumInAccount',
            },
            {
              id: '17',
              morenName: '采购运费',
              zidingyiName: '',
              morenState: true,
              zidingyiState: true,
              morenSort: 16,
              zidingyiSort: 16,
              noModify: false,
              props: 'transportFee',
            },
          ],
        },
        {
          props: 'qjspck',
          label: '期间商品出库',
          subset: [
            {
              id: '1',
              morenName: '采购退货数量',
              zidingyiName: '',
              morenState: true,
              zidingyiState: true,
              morenSort: 0,
              zidingyiSort: 0,
              noModify: false,
              props: 'buyBackOutNumber',
            },
            {
              id: '2',
              morenName: '采购退货金额',
              zidingyiName: '',
              morenState: true,
              zidingyiState: true,
              morenSort: 1,
              zidingyiSort: 1,
              noModify: false,
              props: 'buyBackOutAccount',
            },
            {
              id: '3',
              morenName: '采购换货出库数量',
              zidingyiName: '',
              morenState: true,
              zidingyiState: true,
              morenSort: 2,
              zidingyiSort: 2,
              noModify: false,
              props: 'buyExchangeOutNumber',
            },
            {
              id: '4',
              morenName: '采购换货出库金额',
              zidingyiName: '',
              morenState: true,
              zidingyiState: true,
              morenSort: 3,
              zidingyiSort: 3,
              noModify: false,
              props: 'buyExchangeOutAccount',
            },
            {
              id: '5',
              morenName: '销售出库数量',
              zidingyiName: '',
              morenState: true,
              zidingyiState: true,
              morenSort: 4,
              zidingyiSort: 4,
              noModify: false,
              props: 'saleOutNumber',
            },
            {
              id: '6',
              morenName: '销售出库金额',
              zidingyiName: '',
              morenState: true,
              zidingyiState: true,
              morenSort: 5,
              zidingyiSort: 5,
              noModify: false,
              props: 'saleOutAccount',
            },
            {
              id: '7',
              morenName: '销售换货出库数量',
              zidingyiName: '',
              morenState: true,
              zidingyiState: true,
              morenSort: 6,
              zidingyiSort: 6,
              noModify: false,
              props: 'saleExchangeOutNumber',
            },
            {
              id: '8',
              morenName: '销售换货出库金额',
              zidingyiName: '',
              morenState: true,
              zidingyiState: true,
              morenSort: 7,
              zidingyiSort: 7,
              noModify: false,
              props: 'saleExchangeOutAccount',
            },
            {
              id: '9',
              morenName: '报损数量',
              zidingyiName: '',
              morenState: true,
              zidingyiState: true,
              morenSort: 8,
              zidingyiSort: 8,
              noModify: false,
              props: 'lossOutNumber',
            },
            {
              id: '10',
              morenName: '报损金额',
              zidingyiName: '',
              morenState: true,
              zidingyiState: true,
              morenSort: 9,
              zidingyiSort: 9,
              noModify: false,
              props: 'lossOutAccount',
            },
            {
              id: '11',
              morenName: '调拨出库数量',
              zidingyiName: '',
              morenState: true,
              zidingyiState: true,
              morenSort: 10,
              zidingyiSort: 10,
              noModify: false,
              props: 'allotOutNumber',
            },
            {
              id: '12',
              morenName: '调拨出库金额',
              zidingyiName: '',
              morenState: true,
              zidingyiState: true,
              morenSort: 11,
              zidingyiSort: 11,
              noModify: false,
              props: 'allotOutAccount',
            },
            {
              id: '13',
              morenName: '调价出库数量',
              zidingyiName: '',
              morenState: true,
              zidingyiState: true,
              morenSort: 12,
              zidingyiSort: 12,
              noModify: false,
              props: 'priceChangeOutNumber',
            },
            {
              id: '14',
              morenName: '调价出库金额',
              zidingyiName: '',
              morenState: true,
              zidingyiState: true,
              morenSort: 13,
              zidingyiSort: 13,
              noModify: false,
              props: 'priceChangeOutAccount',
            },
            {
              id: '15',
              morenName: '出库数量合计',
              zidingyiName: '',
              morenState: true,
              zidingyiState: true,
              morenSort: 14,
              zidingyiSort: 14,
              noModify: false,
              props: 'sumOutNumber',
            },
            {
              id: '16',
              morenName: '出库金额合计',
              zidingyiName: '',
              morenState: true,
              zidingyiState: true,
              morenSort: 15,
              zidingyiSort: 15,
              noModify: false,
              props: 'sumOutAccount',
            },
          ],
        },
        {
          props: 'zzrqkc',
          label: '终止日期库存',
          subset: [
            {
              id: '1',
              morenName: '期末数量',
              zidingyiName: '',
              morenState: true,
              zidingyiState: true,
              morenSort: 0,
              zidingyiSort: 0,
              noModify: false,
              props: 'finalNumber',
            },
            {
              id: '2',
              morenName: '期末金额',
              zidingyiName: '',
              morenState: true,
              zidingyiState: true,
              morenSort: 1,
              zidingyiSort: 1,
              noModify: false,
              props: 'finalAccount',
            },
            {
              id: '3',
              morenName: '期末运费',
              zidingyiName: '',
              morenState: true,
              zidingyiState: true,
              morenSort: 2,
              zidingyiSort: 2,
              noModify: false,
              props: 'finalTransportFee',
            },
          ],
        },
      ],
      defaultColumnData: [],// 默认列配置
      columnKey: '商品进销存表',// 列配置的标识key
      updateId: '', // 修改列配置时返回的id
      lookupValue: '', //类别查找值
      // 类别数据
      treeData: [],
      batchData: [], // 存储多选的数据
      showLiepeizhi: false, //是否显示列配置
      isGroupByGoodsType: false,
      total: 0,
      activeModel: {},
      url: {
        exportXlsUrl: '/xghStorehouseGoods/xghStorehouseGoods/exportGoodsOutSaleStoreXls'
      }, // 导出地址
      billName: '商品进销存表',
      printClick: false,
    }
  },
  methods: {
    // 打印
    async handleShortcutKeyPrint() {
      // 查询
      let data = {
        ...this.queryData,
        groupByGoodsTypeFlag: this.isGroupByGoodsType ? '1' : '0',
        pageNo: 1,
        pageSize: 1000000,
      }
      let printExclusive = []
      let res = await getAction('/xghStorehouseGoods/xghStorehouseGoods/goodsOutSaleStore', data)
      if (res.code === 200) {
        printExclusive = res.result.records || []
        printExclusive.forEach((item, index) => {
          item.xh = index + 1
        })
        const pricearr = []
        const monarr =  [
          'beforeAccount', 'beforeTransportFee', 'buyInAccount', 'buyExchangeInAccount', 'saleBackInAccount',
          'saleExchangeInAccount', 'beyondInAccount', 'allotInAccount', 'priceChangeInAccount', 'sumInAccount',
          'transportFee', 'buyBackOutAccount', 'buyExchangeOutAccount', 'saleOutAccount', 'saleExchangeOutAccount',
          'lossOutAccount', 'allotOutAccount', 'priceChangeOutAccount', 'sumOutAccount', 'finalAccount',
          'finalTransportFee'
        ]
        const numarr = ['beforeNumber', 'buyInNumber', 'buyExchangeInNumber', 'saleBackInNumber', 'saleExchangeInNumber',
          'beyondInNumber', 'allotInNumber', 'priceChangeInNumber', 'sumInNumber', 'buyBackOutNumber', 'buyExchangeOutNumber',
          'saleOutNumber', 'saleExchangeOutNumber', 'lossOutNumber', 'allotOutNumber', 'priceChangeOutNumber', 'sumOutNumber',
          'finalNumber', 'allotOutNumber']
        printExclusive = (res.result.records || []).map((item) => {
          // 遍历 item 对象的键
          Object.keys(item).map((key) => {
            if (numarr.includes(key)) {
              item[key] = formatNumber(item[key])
            } else if (monarr.includes(key)) {
              item[key] = formatMoney(item[key])
            } else if (pricearr.includes(key)) {
              item[key] = formatPrice(item[key])
            }
          })
          return item
        })
      }
      this.onQuery()
      // 打印
      if (!this.buttonsPermissions.DaYin) {
        this.$elMessage.warning('未拥有打印权限！')
      } else if (!printExclusive || printExclusive.length <= 0) {
        this.$elMessage.warning('没有要打印的记录！')
      } else {
        let printData = {
          riqi: this.queryData.startDate.split(' ')[0] + "~" + this.queryData.finishDate.split(' ')[0],
          cangku: this.$refs.cangku.selected.label,
          printExclusive,
        }
        // 可以多次打印设置
        if (this.printPermissions) {
          this.$refs.print.show(printData, this.columnData, this.billName)
        } else {
          if (this.$lcStorage.get('printLimitValue')) {
            // 获取打印次数
            this.getPrintNum(this.billName, (() => {
              this.$refs.print.show(printData, this.columnData, this.billName)
            }))
          } else {
            this.$refs.print.show(printData, this.columnData, this.billName)
          }
        }
      }
    },
    initData() {
      this.monthValue = '本月'
      this.getTimeRange(this.$setTimeRange(5))
      // 获取商品类别列表
      this.getTreeData()
      // 获取仓库信息
      this.checkStorehouse()
      this.requestQuery()
      // 初始化列配置数据
      this.defaultColumnData = JSON.parse(JSON.stringify(this.columnData))
      this.selectTitle()
    },
    // 刷新
    onRefresh() {
      this.initData()
      this.getTreeData()
      Object.assign(this.$data, this.$options.data())
      this.monthValue = '本月'
      this.getTimeRange(this.$setTimeRange(5))
      // 初始化列配置数据
      this.defaultColumnData = JSON.parse(JSON.stringify(this.columnData))
      this.selectTitle()
      this.requestQuery()
    },
    // 查询
    onQuery() {
      this.queryData.pageNo = 1
      this.requestQuery()
    },
    // 请求筛选条件数据
    requestQuery() {
      let data = {
        ...this.queryData,
        groupByGoodsTypeFlag: this.isGroupByGoodsType ? '1' : '0'
      }
      getAction('/xghStorehouseGoods/xghStorehouseGoods/goodsOutSaleStore', data).then((res) => {
        if (res.code === 200) {
          this.total = res.result.total
          let data = res.result.records || []
          data.forEach((item, index) => {
            item.xh = index + 1
            // 数据显示格式转换成系统类型
            for (const key in item) {
              if (['beforeNumber', 'buyInNumber', 'buyExchangeInNumber', 'saleBackInNumber', 'saleExchangeInNumber',
                'beyondInNumber', 'allotInNumber', 'priceChangeInNumber', 'sumInNumber', 'buyBackOutNumber', 'buyExchangeOutNumber',
                'saleOutNumber', 'saleExchangeOutNumber', 'lossOutNumber', 'allotOutNumber', 'priceChangeOutNumber', 'sumOutNumber',
                'finalNumber'].includes(key)) {
                item[key] = formatNumber(item[key])
              } else if (['beforeAccount', 'beforeTransportFee', 'buyInAccount', 'buyExchangeInAccount', 'saleBackInAccount',
                'saleExchangeInAccount', 'beyondInAccount', 'allotInAccount', 'priceChangeInAccount', 'sumInAccount', 'transportFee',
                'buyBackOutAccount', 'buyExchangeOutAccount', 'saleOutAccount', 'saleExchangeOutAccount', 'lossOutAccount',
                'allotOutAccount', 'priceChangeOutAccount', 'sumOutAccount', 'finalAccount', 'finalTransportFee'].includes(key)) {
                item[key] = formatMoney(item[key])
              }
            }
          })
          this.tableData = data
        }
      })
    },
    // 判断当前选择时间段
    changeDate(val) {
      this.isShowDate = true
      switch (val) {
        case '全部':
          this.isShowDate = false
          break
        case '今日':
          this.getTimeRange(this.$setTimeRange(1))
          break
        case '昨天':
          this.getTimeRange(this.$setTimeRange(2))
          break
        case '本周':
          this.getTimeRange(this.$setTimeRange(3))
          break
        case '上周':
          this.getTimeRange(this.$setTimeRange(4))
          break
        case '本月':
          this.getTimeRange(this.$setTimeRange(5))
          break
        case '上月':
          this.getTimeRange(this.$setTimeRange(6))
          break
        case '本年':
          this.getTimeRange(this.$setTimeRange(7))
          break
        case '去年':
          this.getTimeRange(this.$setTimeRange(8))
          break
        default:
          break
      }
    },
    // 获取时间范围并设置
    getTimeRange(data) {
      this.nowDate = [data.now, data.end]
      this.queryData.startDate = data.now + ' 00:00:00'
      this.queryData.finishDate = data.end + ' 23:59:59'
    },
    // 获取当前选中时间
    onClickPicker(val) {
      this.queryData.startDate = val[0] + ' 00:00:00'
      this.queryData.finishDate = val[1] + ' 23:59:59'
    },
    // 从后台获取数据,重新排序
    changeSort(val) {
      console.log(val) // column: {…} order: "ascending" prop: "date"
      // 根据当前排序重新获取后台数据,一般后台会需要一个排序的参数
    },
    // 触发表格多选按钮
    handleSelectionChange(val) {
      this.batchData = val
    },
    // 触发列配置弹框
    onShowColumn() {
      // 现在的列配置
      let list = []
      this.columnData.forEach(item => {
        let sub = item.subset
        list = list.concat(sub)
      })
      // 默认的列配置
      let defaultList = []
      this.defaultColumnData.forEach(item => {
        let sub = item.subset
        defaultList = defaultList.concat(sub)
      })
      this.$refs.setColumnRef.loadData(list, defaultList)
    },
    // 查询保存的列配置
    async selectTitle() {
      let obj = await this.$setColumn.selectColumn(this.columnKey)
      if (obj.code === 200) {
        this.updateId = obj.updateId
        this.columnData = obj.columnData
      } else if (obj.code === 500) {
        let id = await this.$setColumn.addColumn(this.columnKey, this.defaultColumnData, 'props')
        this.updateId = id
        this.columnData = this.defaultColumnData
      } else if (obj.code === 404) {
        this.columnData = this.defaultColumnData
      }
    },
    // 修改列配置后保存修改后的值
    async onSaveColumn(value) {
      let nIndex = 0
      this.columnData.forEach((lItem, lIndex) => {
        let subList = lItem.subset
        if (lIndex > 0) {
          nIndex += this.columnData[lIndex - 1].subset.length
        }
        let list = []
        let count = 0 //记录隐藏的个数
        subList.forEach((item, index) => {
          let nItem = value[nIndex + index]
          list.push(nItem)
          if (!nItem.zidingyiState) {
            count++
          }
        })
        lItem.subset = list
        lItem.show = !(count == list.length)
        this.columnData.splice(lIndex, 1, lItem)
      })
      this.onSaveColumnRequest()
    },
    // 修改列配置后保存修改后的值
    async onSaveColumnRequest() {
      let res = await this.$setColumn.editColumn(this.columnKey, this.columnData, this.updateId)
      if (res.code == 200) {
        this.showTips(res.message, 0)
      } else {
        this.showTips(res.message, 2)
      }
    },
    // 合计列计算
    getSummaries(param) {
      const { columns, data } = param
      const sums = []
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '合计'
        } else if (index === 1) {
          sums[index] = '共' + this.total + '条明细'
        } else if (
          ['beforeAccount', 'beforeTransportFee', 'buyInNumber', 'buyInAccount',
            'buyExchangeInNumber', 'buyExchangeInAccount', 'saleBackInNumber', 'saleBackInAccount',
            'saleExchangeInNumber', 'saleExchangeInAccount', 'beyondInNumber', 'beyondInAccount',
            'sumInNumber', 'sumInAccount', 'transportFee', 'buyBackOutNumber', 'buyBackOutAccount',
            'buyExchangeOutNumber', 'buyExchangeOutAccount', 'saleOutNumber', 'saleOutAccount',
            'saleExchangeOutNumber', 'saleExchangeOutAccount', 'sumOutNumber', 'sumOutAccount',
            'finalNumber', 'finalAccount', 'finalTransportFee', 'beforeNumber', 'priceChangeOutAccount',
            'allotInNumber', 'allotInAccount', 'priceChangeInNumber', 'priceChangeInAccount',
            'lossOutNumber', 'lossOutAccount', 'allotOutAccount', 'priceChangeOutNumber', 'allotOutNumber'
          ].includes(column.property)
        ) {
          const values = data.map((item) => delcommafy(item[column.property]))
          if (!values.every((value) => isNaN(value))) {
            sums[index] = values.reduce((prev, curr) => {
              const value = delcommafy(curr)
              if (!isNaN(value)) {
                return prev + curr
              } else {
                return prev
              }
            }, 0)
            if (['beforeNumber', 'buyInNumber', 'buyExchangeInNumber', 'saleBackInNumber', 'saleExchangeInNumber',
              'beyondInNumber', 'allotInNumber', 'priceChangeInNumber', 'sumInNumber', 'buyBackOutNumber', 'buyExchangeOutNumber',
              'saleOutNumber', 'saleExchangeOutNumber', 'lossOutNumber', 'allotOutNumber', 'priceChangeOutNumber', 'sumOutNumber',
              'finalNumber', 'allotOutNumber'].includes(column.property)) {
              sums[index] = formatNumber(sums[index]) // 保留2位小数，解决小数列合计精度缺失的问题
            } else {
              sums[index] = formatMoney(sums[index]) // 保留2位小数，解决小数列合计精度缺失的问题
            }
          }
        } else {
          sums[index] = ''
        }
      })
      return sums
    },
    /**
    * tree树被点击
    */
    handleNodeClick(rowData) {
      this.queryData.goodsTypeId = rowData.id
      // 点击的每一个tree树的信息对象
      this.queryData.pageNo = 1
      this.requestQuery()
    },
    //类别筛选
    filterNode(value, data) {
      if (!value) return true
      return data.label.indexOf(value) !== -1
    },
    /**
     * 获取tree数据
     */
    getTreeData() {
      getAction('/xghGoodsType/xghGoodsType/typeList', {}).then((res) => {
        if (res.code === 200) {
          let list = res.result
          let result = this.getTreeDataByList(list, '0', '')
          this.treeData = [{
            id: '',
            label: '全部',
            children: result
          }]
        }
      })
    },
    getTreeDataByList(list, parentId, parentName) {
      let data = []
      for (let index = 0; index < list.length; index++) {
        const element = list[index];
        if (element.pid == parentId) {
          data.push({
            ...element,
            id: element.id,
            label: element.goodsTypeName,
            parentId,
            parentName,
            children: this.getTreeDataByList(list, element.id, element.companyTypeName)
          })
        }
      }
      return data
    },
    /**
     * 获取仓库列表数据
     */
    checkStorehouse() {
      getAction('/xghStorehouse/xghStorehouse/queryListByPole', {}).then((res) => {
        if (res.code === 200) {
          this.ckOptions = res.result
          this.ckOptions.splice(0, 0, {
            storehouseName: '全部',
            id: '',
            "storehousePinyinCode": "QB"
          })
          this.totalStores = this.ckOptions
        } else {
          this.showTips(res.message, 2)
        }
      })
    },
    // 获取每页几条数据
    handleSizeChange(val) {
      this.queryData.pageSize = val
      this.requestQuery()
    },
    // 获取当前第几页
    handleCurrentChange(val) {
      this.queryData.pageNo = val
      this.requestQuery()
    },
    // 获取当前第几页
    typeChange() {
      this.queryData.pageNo = 1
      this.requestQuery()
    },
    // 表格单选
    rowClick(row, column, event) {
      this.activeModel = row
    },
    // 仓库切换方法
    onChangeStorehouse(val) {
      this.ckOptions.filter((item) => {
        if (item.id === val) {
          this.storehouseName = item.storehouseName
        }
      })
    },
    /**
    * 处理表格每一个行
    */
    handleRowDbclick(row) {
      if (this.isGroupByGoodsType) {
        return this.showTips('商品类别结果不能查看明细！')
      }
      this.$router.push({
        name: 'reportStatistics-inventoryReport-inventoryChange', //库存变更明细
        params: {
          goodsId: row.goodsId,
          goodsName: row.goodsId_dictText,
          storehouseId: this.queryData.storehouseId,
          storehouseName: this.storehouseName
        },
      })
    },
    // 导出
    exportExcel() {
      const fileName = '商品进销存表'
      let params = {
        ...this.queryData,
        groupByGoodsTypeFlag: this.isGroupByGoodsType ? '1' : '0',
        fileName,
      }
      this.handleExportXlsByStaff(fileName, params)
    },
    // 查看明细
    onLookDetails() {
      if (JSON.stringify(this.activeModel) == "{}") {
        this.activeModel = this.tableData[0]
      }
      this.handleRowDbclick(this.activeModel)
    },
    showTips(msg, type = 1) {
      switch (type) {
        case 0:
          this.$elMessage.success(msg)
          break;
        case 1:
          this.$elMessage.warning(msg)
          break;
        case 2:
          this.$elMessage.error(msg)
          break;
      }
    },
    // table 动态赋值
    commonFormat(row, props) {
      return row[props]
    },
    goodsNumberFormat(row) {
      return this.isGroupByGoodsType ? '分类' : row.goodsNumber
    },
    goodsNameFormat(row) {
      return this.isGroupByGoodsType ? row.goodsName_dictText : row.goodsId_dictText
    },
    finalNumberFormat(row) {
      // 期末数量 = 此前数量 + 入库数量合计 - 出库数量合计
      return formatNumber(Number(row.beforeNumber) + Number(row.sumInNumber) - Number(row.sumOutNumber))
    },
    finalAccountFormat(row) {
      // 期末金额 = 此前金额 + 出库金额 - 入库金额
      return formatPrice(Number(row.beforeAccount) + Number(row.sumOutAccount) - Number(row.sumInAccount))
    },
    // 搜索
    handleFilterMethod(value, label) {
      if (label == 'storehouseName') {
        this.ckOptions = this.filterMethod(this.ckOptions, value, label)
      }
    },
  },
}
</script>

<style lang="scss" scoped>
$font-size: 12px;
$height-input: 30px; //0.55rem的高度
$height: 50px; //1rem的高度

.basic_info {
  width: 100%;
  height: calc(100vh - 1.875117rem);
  background: #f0f2f5;
  box-sizing: border-box;
  padding: 0 0.168761rem 0.168761rem;
  display: flex;
  flex-direction: column;
}

.info_operation_btn {
  width: 100%;
  height: $height;
  display: flex;
  align-items: center;

  ::v-deep .el-button {
    width: 80px;
    height: $height-input;
    text-align: center;
    box-sizing: border-box;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: $font-size;
    border-radius: 0.1rem;
    border: none;
    margin-right: 6px;
    margin-left: 0;
  }

  .el-button--default {
    color: #666;
  }

  ::v-deep .el-select {
    width: 1rem;
    height: $height-input;
    box-sizing: border-box;
    margin-right: 0.112507rem;

    .el-input {
      .el-input__inner {
        height: $height-input;
        border: none;
        font-size: $font-size;
      }

      .el-input__suffix {
        height: $height-input;
        display: flex;
        align-items: center;
      }
    }
  }

  .btn_left {
    width: 60%;
    display: flex;
    justify-content: flex-start;
    align-items: center;
  }

  .btn_right {
    width: 40%;
    display: flex;
    justify-content: flex-end;
    align-items: center;

    .el-input {
      width: 4.725295rem;
      height: $height-input;
      margin-right: 0.112507rem;

      ::v-deep .el-input__inner {
        height: $height-input;
        border: none;
        box-sizing: border-box;
        padding: 0 0.15rem;
        background: #fff;
        font-size: $font-size;
        color: #666;

        &::placeholder {
          font-size: $font-size;
          color: #c1c1c1;
        }
      }
    }

    .el-button {
      width: 1rem;
    }

    .set_up {
      font-size: 20px;
      color: rgb(153, 153, 153);
    }
  }
}

.info_content_div {
  width: 100%;
  height: calc(100% - 1rem);
  background: #fff;
  box-sizing: border-box;
  padding: 0 0.168761rem;
  display: flex;
  flex-direction: column;

  .div_screen {
    width: 100%;
    height: $height-input;
    display: flex;
    justify-content: space-between;
    box-sizing: border-box;
    margin-top: 15px;

    ::v-deep .el-select {
      width: 2.250141rem;
      height: $height-input;
      box-sizing: border-box;
      border-radius: 4px;

      .el-input {
        .el-input__inner {
          height: $height-input;
          border-color: #e3e3e3;
          font-size: $font-size;
          padding-left: 0.15rem;
        }

        .el-input__suffix {
          height: $height-input;
          display: flex;
          align-items: center;
        }
      }
    }

    input {
      width: 2.250141rem;
      height: 30px;
      background: #ffffff;
      border: 1px solid #e3e3e3;
      opacity: 1;
      border-radius: 4px;
      font-size: $font-size;
      color: #666;
      box-sizing: border-box;
      padding: 0 0.15rem;

      &:focus {
        outline: none;
        border-color: #1890ff;
      }
    }

    ::v-deep .el-checkbox {
      display: flex;
      align-items: center;
      margin-right: 0.25rem;

      .el-checkbox__input {
        height: 14px;
      }

      .el-checkbox__label {
        font-size: $font-size;
        padding-left: 0.1rem;
        color: #666;
      }

      .is-checked {
        .el-checkbox__inner {
          background: #1a93fa;
          border-color: #1a93fa;
        }
      }
    }

    .screen_left {
      height: 100%;
      box-sizing: border-box;
      display: flex;
      justify-content: flex-start;
      flex-wrap: wrap;

      >div {
        width: 176px;
        height: $height-input;
        box-sizing: border-box;
        display: flex;
        align-items: center;
        margin-right: 14px;

        >p {
          margin: 0;
          width: auto;
          min-width: 50px;
          margin-right: 6px;
          font-size: $font-size;
          font-family: PingFang SC;
          font-weight: 400;
          color: #666666;
          text-align: justify;
          text-justify: distribute-all-lines;
          /*ie6-8*/
          text-align-last: justify;
          /* ie9*/
          -moz-text-align-last: justify;
          /*ff*/
          -webkit-text-align-last: justify;
          /*chrome 20+*/
        }

        .screen_select {
          flex: 1;
          display: flex;
          align-items: center;
        }
      }
    }

    .screen_right {
      height: 100%;
      display: flex;
      flex-direction: column;

      >div {
        width: 100%;
        // height: 50%;
        height: $height-input;
        box-sizing: border-box;
        display: flex;
        justify-content: flex-end;
        // justify-content: space-between;
        // align-items: flex-start;
        align-items: center;

        p {
          margin: 0;
          font-size: $font-size;
          font-family: PingFang SC;
          font-weight: 400;
          color: #fd6c21;
          margin-right: 0.168761rem;
        }

        ::v-deep .el-button {
          width: 60px;
          height: $height-input;
          display: flex;
          align-items: center;
          justify-content: center;
          font-size: $font-size;
          border-radius: 0.1rem;
          border: none;
        }

        ::v-deep .el-select {
          width: 80px;
          margin-right: 6px;
        }

        ::v-deep .el-date-editor {
          width: 178px;
          height: $height-input;
          background: #ffffff;
          border: 1px solid #d9d9d9;
          margin-right: 10px;
          padding: 3px 6px;

          .el-range__icon {
            position: absolute;
            right: 0;
            line-height: 30px;
          }

          .el-range-separator {
            line-height: 24px;
          }

          .el-range-input {
            font-size: $font-size;
          }

          .el-range__close-icon {
            display: none;
          }
        }
      }

      >div:nth-child(2) {
        justify-content: flex-start;
      }
    }
  }

  .div_table {
    width: 100%;
    height: calc(100% - 120px);
    margin-top: 14px;
    display: flex;
    box-sizing: border-box;

    .div_tree {
      width: 2.812676rem;
      height: 100%;
      box-sizing: border-box;
      padding: 0.131258rem 0;
      border: 0.018751rem solid #e3e3e3;
      transition: all 0.3s;

      .tree_div1 {
        width: 100%;
        height: 0.337521rem;
        box-sizing: border-box;
        padding: 0 0.168761rem;
        display: flex;
        align-items: center;
        justify-content: space-between;

        p {
          color: #666666;
          font-size: $font-size;
        }
      }

      .tree_input {
        width: 100%;
        height: $height-input;
        margin-top: 6px;
        box-sizing: border-box;
        padding: 0 0.168761rem;

        p {
          width: 100%;
          height: $height-input;
          margin: 0;
          box-sizing: border-box;
          border: 1px solid #dddddd;
          display: flex;
          align-items: center;
          position: relative;

          input {
            width: 100%;
            box-sizing: border-box;
            padding: 0 0.600038rem 0 0.1rem;
            height: 25px;
            border: none;
            font-size: $font-size;

            &:focus {
              outline: none;
            }
          }

          span {
            position: absolute;
            right: 0;
            display: flex;
            align-items: center;
            justify-content: center;
            width: 20px;
            height: 20px;
            margin-right: 4px;
            background: #fff;

            .el-icon-search {
              font-size: 17px;
              color: #d3d3d3;
            }
          }
        }
      }

      .tree_structure {
        width: 100%;
        height: calc(100% - 0.337521rem - 0.131258rem - 0.131258rem - 0.55rem);
        box-sizing: border-box;
        padding: 0 0.168761rem;
        margin-top: 0.131258rem;
        overflow: auto;

        ::v-deep .el-tree {
          width: 100%;

          .el-tree-node__children {
            .el-tree-node__label {
              color: #666;
              font-weight: normal;
            }
          }

          .el-tree-node__label {
            font-size: $font-size;
            font-weight: 600;
          }

          .el-tree-node__content>.el-tree-node__expand-icon {
            padding-left: 0;
          }
        }
      }
    }

    .table_content {
      width: calc(100% - 3.000188rem);
      margin-left: 0.187512rem;
      height: 100%;

      ::v-deep .el-table {
        border: 1px solid #e3e3e3;
        box-sizing: border-box;

        th,
        td {
          padding: 0;
          color: #666;
          box-sizing: border-box;
          border-right: 1px solid #e3e3e3;

          &:first-child {
            .cell {
              display: flex;
              justify-content: center;
            }
          }

          &:last-child {
            border-right: none;
          }
        }

        .el-table-column--selection {
          .cell {
            padding: 0;
            text-align: center;
          }
        }

        .el-table__header {
          tr {
            &:last-child {
              th {
                background: rgb(237, 237, 237) !important;
              }
            }
          }
        }

        .cell {
          white-space: nowrap;
          line-height: $height-input;
          font-size: $font-size;
        }

        // th选择框颜色修改
        .el-checkbox {
          .is-indeterminate {
            .el-checkbox__inner {
              color: #1b93f9;
              background-color: #1b93f9;
              border-color: #1b93f9;
            }
          }
        }

        // td选择框颜色修改
        .is-checked {
          .el-checkbox__inner {
            color: #1b93f9;
            background-color: #1b93f9;
            border-color: #1b93f9;
          }
        }

        .el-table__body tr:hover>td {
          background: #e3f6ff;
        }

        .el-table__footer-wrapper {
          td {
            background: #f4f4f4;
            border: none;
          }
        }

        //滚动条的宽度
        ::-webkit-scrollbar {
          width: 8px;
          height: 10px;
          background-color: #e5e5e5;
        }

        //滚动条的滑块
        ::-webkit-scrollbar-thumb {
          background-color: #c9c9c9;
        }
      }
    }
  }

  .table_pagination {
    width: 100%;
    height: $height-input;
    box-sizing: border-box;
    margin-top: 20px;
    display: flex;

    .pagination_div {
      width: 100%;
      display: flex;
      align-items: center;
      justify-content: flex-end;

      ::v-deep .el-pagination {
        padding: 0;
        height: 0.525033rem;

        .el-pagination__sizes {
          margin: 0;

          .el-input {
            margin-right: 0;
          }
        }
      }
    }
  }
}
</style>